import Routes from '@/router/route'
import { RouteRecordRaw } from 'vue-router'

const token = localStorage.getItem('token')


export default {
    namespaced : false,
    state : () => ({
        token : token,
        auth : token != null
    }),

    mutations : {
        setAuth(state) {
            state.auth = !state.auth
        },

        setToken(state, token) {
            state.token = token
            if(token) {
                localStorage.setItem('token', token)
            }else {
                localStorage.removeItem('token')
            }
            
        },
    },
    
    actions : {
        login({commit}, payload) {
            commit('setToken', payload.token)
            commit(
                'setUserInfo',
                {currentAuthority : payload.currentAuthority, email : payload.email, head_portrait : payload.head_portrait, username : payload.username, user_id : payload.user_id},
                {root : true}
            )
            commit('setAuth')
        },

        logout({commit}) {
            commit('setToken', null)
            commit('setUserInfo', null, {root : true})
            commit('setAuth')
        }
    },
}